package com.lkhhd.user.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.lkhd.common.exception.ActivityException;
import com.lkhd.common.myemum.ActivityEnum;
import com.lkhd.common.myemum.UserEnum;
import com.lkhd.manage.mapper.ActivityMapper;
import com.lkhd.manage.mapper.CollectionMapper;
import com.lkhd.manage.mapper.TicketMapper;
import com.lkhd.manage.mapper.UserMapper;
import com.lkhd.manage.pojo.Activity;
import com.lkhd.manage.pojo.Collection;
import com.lkhd.manage.pojo.Ticket;
import com.lkhd.manage.pojo.User;
import com.lkhd.user.service.UserService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * UserService服务实现
 * 
 * @author youjie 2018-9-14 10:44:35
 */
@Service(version="1.0.0")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CollectionMapper collectionMapper;
	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private TicketMapper ticketMapper;
	/**
	 * 添加用户
	 */
	@Override
	public int addUser(User user) {
		return userMapper.insert(user);
	}

	/**
	 * 拉黑冻结用户
	 * 
	 * @param user
	 * @param is   true拉黑 false恢复
	 * @return
	 */
	@Override
	public int freeze(User user, boolean is) {
		user.setStatus(is == true ? UserEnum.FREEZE.getCode() : UserEnum.NORMAL_USER.getCode());
		return this.updateUser(user);
	}

	/**
	 * 更新用户
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public int updateUser(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}

	/**
	 * 根据userId查询用户
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public User findByUserId(Long userId) {
		User user = userMapper.selectByPrimaryKey(userId);
		return user;
	}

	/**
	 * 根据条件查询用户列表
	 * 
	 * @param example  条件
	 * @param currPage 当前页
	 * @param pageSize 每页大小
	 * @return 返回符合条件的用户
	 */
	@Override
	public List<User> selectAll(Example example, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return userMapper.selectByExample(example);
	}

	/**
	 * 用户收藏活动
	 * 
	 * @param collection 要包括用户id,活动id
	 * @return
	 * @throws ActivityException
	 */
	@Override
	public int collectionActivity(Collection collection) {
		// 用户从 redis中获取登录的用户，不存在没有改用户。controller判断用户是否存在
//		User user = this.findByUserId(collection.getUserid());
		Collection querycollection = new Collection();
		querycollection.setUserid(collection.getUserid());
		querycollection.setActivityid(collection.getActivityid());
		// 是否已经收藏
		int selectCount = collectionMapper.selectCount(querycollection);
		if (selectCount > 0) {
			throw new ActivityException(ActivityEnum.DUPLICATE_COLLECTION);
		}
		// 是否有改活动
		Activity dbActivity = activityMapper.selectByPrimaryKey(collection.getActivityid());
		if (dbActivity == null) {
			throw new ActivityException(ActivityEnum.NOT_ACTIVITY);
		}
		return collectionMapper.insert(collection);
	}

	/**
	 * 用户删除收藏活动
	 * 
	 * @param collection
	 * @return
	 */
	@Override
	public int cancelCollection(Collection collection) {
		// controller 要设置userId（登录用户id,自己的用户id，不然可能是别人的收藏）
		// 根据collId和userID判断是否有该收藏记录
		int selectCount = collectionMapper.selectCount(collection);
		if (selectCount <= 0) {
			throw new ActivityException(ActivityEnum.NOTCOLLECTION);
		}
		return collectionMapper.delete(collection);
	}

	/**
	 * 用户是否收藏改活动
	 * 
	 * @param collection collection 要包括用户id,活动id
	 * @return
	 */
	@Override
	public boolean userIsCollection(Collection collection) {
		// 根据activiID和userID判断是否有该收藏记录
		int selectCount = collectionMapper.selectCount(collection);
		return selectCount>0?true:false;
	}

	/**
	 * 用户收藏活动列表
	 * 
	 * @param user 根据用户id获取用户收藏活动列表
	 * @param pageNum 页码
	 * @param pageSzie 每页大小
	 * @return
	 */
	@Override
	public List<Collection> collectionActivityList(User user,Integer pageNum, Integer pageSzie) {
		Example example = new Example(Collection.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userid", user.getUserid());
		
		PageHelper.startPage(pageNum, pageSzie);
		List<Collection> colls = collectionMapper.selectByExample(example);
		return colls;
	}

	/**
	 *  根据用户id查询门票购买记录列表
	 * 
	 * @param user
	 * @param status 门票状态  	 0待支付 1已支付待参加 2已参加未评价 3已参加已评价 4过期
	 * @return
	 */
	@Override
	public List<Ticket> selectTicket(User user, int status,Integer pageNum, Integer pageSzie) {
		Example example = new Example(Ticket.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userid", user.getUserid());
		criteria.andEqualTo("status", status);
		
		PageHelper.startPage(pageNum, pageSzie);
		List<Ticket> tickets = ticketMapper.selectByExample(example);
		return tickets;
	}

}
